<?php
use yii\helpers\Html;
use \backend\controllers\DtActionFilter;

echo Html::cssFile('@web/css/select2.min.css');
$project_id = Yii::$app->request->get('project_id',0);
if (!empty($project_id)){
    $isView = DtActionFilter::checkProject('project_edit',$project_id);
}else{
    $isView = false;
}
?>

<style type="text/css">
.select2-container {
width: 100%!important;
}
</style>
<div class="container containerShadow diyawoW">
    <div class="row">
        <div class="topimg">
            <div class="left pull-left">
                <span>抵押物情况</span>
            </div>
            <div class="pull-right">
                <a href="javascript:;">
                    <!--<img src="image/projectDetails/edit.png">-->
                </a>
            </div>
        </div>
        <!--<p class="diYaList">抵押物列表</p>-->
        <div class="topimg">

            <div class="centerText clearfix ">
                <span class="pull-left ">抵押物列表</span>
                <a href="###" class="pull-right">
                    <?php
                    if ($isView){
                        echo Html::img("/image/projectDetails/edit.png",["id"=>"getPingGuData","data-val"=>5]);
                    }
                    ?>
                </a>
            </div>
        </div>
        <?php
        if ($mortgageCount > 0) {

            ?>
            <form action="" class="diyawuW" >
                <table class="tableRaius diYaTable" border="0" cellspacing="0" cellpadding="0"
                       style="border-collapse:separate;border-spacing: 0;">
                    <tr>
                        <td style="border: none;">序号</td>
                        <td style="border: none;">抵押本金</td>
                        <td style="border: none;">抵押利息</td>
                        <td style="border: none;">权属人</td>
                        <td style="border: none;">抵押物类型</td>
                        <td style="border: none;">抵押物编号/他项权证号</td>
                        <td style="border: none;">详细情况</td>
                    </tr>
                    <?php
                    if (!empty($mortgageStateList) && is_array($mortgageStateList)) {
                        $inum = 1;   //table的行数
                        foreach ($mortgageStateList as $k => $v) {
                            $iinum = 1;   //每次循环只加载一次td
                            $tdNum = count($v);
                            foreach ($v as $k1 => $v1) {
                                $mortgage_principal = !empty($v1['mortgage_principal']) ? \common\helps\formatMoney::format_money($v1['mortgage_principal'],2): '';
                                $mortgage_interest = !empty($v1['mortgage_interest']) ? \common\helps\formatMoney::format_money($v1['mortgage_interest'],2) : '';
                                $mortgage_owner = !empty($v1['mortgage_owner']) ? $v1['mortgage_owner'] : '';
                                $mortgage_code = !empty($v1['mortgage_code']) ? $v1['mortgage_code'] : '';
                                $paramsType = \Yii::$app->params['mortgageTypes3'];
                                $dName = '抵押物' . $inum;
                                $typeStr = '';
                                if (!empty($v1['mortgage_type'])) {
                                    $typeArr = explode(',', $v1['mortgage_type']);
                                    if (!empty($typeArr) && is_array($typeArr)) {
                                        foreach ($typeArr as $k => $v) {
                                            if (!empty($paramsType[$v]['name'])){
                                                $typeStr = $typeStr . $paramsType[$v]['name'] . ',';
                                            }
                                        }
                                    }
                                }
                               if (!empty($typeStr)) {
                                    $typeStr = rtrim($typeStr, ',');
                                }

                                if ($iinum == 1) {
                                    $iinum++;
                                    echo <<<TDT
                                <tr>
                    <td rowspan="$tdNum">$inum</td>
                    <td rowspan="$tdNum">
                        <input type="text" readonly="readonly" name="" id="" title="$mortgage_principal" value="$mortgage_principal" />
                    </td>
                    <td rowspan="$tdNum" style="border-right:1px solid #e2e2e2;">
                        <input type="text" readonly="readonly" name="" id="" title="$mortgage_interest" value="$mortgage_interest" />
                    </td>
TDT;
                                }
                                echo <<<DYS

                    <td>
                        <input type="text" readonly="readonly" name="" id="" title="内容提示" value="$mortgage_owner" />
                    </td>
                    <td>
                        <input type="text" readonly="readonly" name="" id="" title="内容提示" value="$typeStr" />
                    </td>
                    <td style="border-right:none!important">
                        <input type="text" readonly="readonly" name="" id="" title="内容提示" value="$mortgage_code" />
                    </td>
                    <td>
                        <a href="javascript:;">$dName</a>
                    </td>
                </tr>
DYS;
                                $inum++;
                            }
                        }
                    }
                    ?>

                </table>

            </form>
            <?php
        } else {
            echo "<p class=\"text-leftC\">无数据</p>";
        }
        ?>


        <form action="" class="diyawuEdit" id="diYaList" style="display: none;">
            <?php
            if (!empty($mortgageStateList) && is_array($mortgageStateList)) {
                $select2IDAdd = 1; //select2ID
                foreach ($mortgageStateList as $k => $v) {
                        $mortgageNum = 1;  //table 行数
                        $mortgageBaseNum = 1; //抵押本金的table只执行一次
                        foreach ($v as $k1 => $v1) {
                            $mortgage_principal = !empty($v1['mortgage_principal']) ? $v1['mortgage_principal'] : '';
                            $mortgage_interest = !empty($v1['mortgage_interest']) ? $v1['mortgage_interest'] : '';
                            $mortgage_owner = !empty($v1['mortgage_owner']) ? $v1['mortgage_owner'] : '';
                            $mortgage_code = !empty($v1['mortgage_code']) ? $v1['mortgage_code'] : '';
                            $mortgage_id = !empty($v1['id']) ? $v1['id'] : 0;
                            $ppesid = !empty($v1['ppesid']) ? $v1['ppesid'] : 0;  //project_pre_evaluation_seal 的ID
                            $typeArr = !empty($v1['mortgage_type']) ? json_encode(explode(',', $v1['mortgage_type'])) : json_encode('');
                            $paramsType = \Yii::$app->params['mortgageTypes3'];
                            $dName = '抵押物' . $inum;
                            $select2Name = 'mortgageSelect' . $select2IDAdd;
                            if (!empty($v1)) {
                                if ($mortgageBaseNum==1){
                                echo <<<DIYABENJIN
                                	<hr style="border:none;border-top:2px dotted #4da1ff;" />
                                 <table class="editTable tableEdit2 tableEditNum">
                                          <tr>
                                                    <td>抵押本金（元）</td>
                                                    <td class="">
                                                        <input type="text" isNumber='true' onkeydown="true" name="" id="benJin1" value="$mortgage_principal" />
                                                    </td>
                                                    <td class="text-center">抵押利息（元）</td>
                                                    <td class="">
                                                        <input type="text" isNumber='true'  onkeydown="true" name="" id="liXi1" value="$mortgage_interest" />
                                                    </td>
                                          </tr>
                                 </table>
                                 <p class="text">请添加本条抵押本金的抵押物信息</p>
                                  <table class="editTable tableEdit3 tableEditInfoNum">
                                           <tr>
                                                    <td>序号</td>
                                                    <td>权属人</td>
                                                <td>抵押物类型</td>
                                                    <td>抵押物编号/他项权证号</td>
                                                    <td>&nbsp;</td>
                                            </tr>
DIYABENJIN;
                                    $mortgageBaseNum++;
                                }

                                echo <<<DOIYATABLE
                <tr>
                    <td>$mortgageNum</td>
                    <td>
                        <input type="text" name="" id="" value="$mortgage_owner" />
                    </td>
                    <td>
                        <select class="form-control select2" data-val='$typeArr' id="$select2Name"  multiple="multiple"  style="width: 100%;">

                        </select>
                    </td>
                    <td>
                        <input type="text" name="" id="" value="$mortgage_code" />
                    </td>
                    <td>
                 		   <input type="text" hidden value="$mortgage_id">
                    </td>
                    <td>
                  		  <input type="text" hidden value="$ppesid">
                    </td>
                    <td>
                        <input type="button" class="remove" data-toggle="modal"  onclick="delMortgageInfo(this)"  title="点击删除" name="" id="" value="删除" />
                        <input type="button" class="add" title="点击添加" name="" id="" value="添加" />
                    </td>
                </tr>
DOIYATABLE;
                            $mortgageNum++;
                            $select2IDAdd++;
                        }
                    }
                    echo "</table>";
                }
                echo <<<DIYAEND
<p class="continueAdd">继续添加抵押本金</p>
            <div class="cancelInConfirm clearfix">
                <input id="ajaxMortgageData" class="cancelInConfirmActive" type="button" value="保存" />
                <input id='cancelMortgageData' type="button" value="取消" />
            </div>
DIYAEND;

            } else {
                echo <<<DIYANULL
     <table class="editTable tableEdit2 tableEditNum">
                <tr>
                    <td>抵押本金</td>
                    <td class="">
                        <input type="text" isNumber='true' onkeydown="true" name="" id="benJin2" value="" />
                    </td>
                    <td class="text-center">抵押利息</td>
                    <td class="">
                        <input type="text" isNumber='true' onkeydown="true" name="" id="liXi2" value="" />
                    </td>
                </tr>
            </table>
            <p class="text">请添加本条抵押本金的抵押物信息</p>
            <table class="editTable tableEdit3 tableEditInfoNum">
                <tr>
                    <td>序号</td>
                    <td>权属人</td>
                    <td>抵押物类型</td>
                    <td>抵押物编号/他项权证号</td>
                    <td>&nbsp;</td>
                </tr>
                <tr>
                    <td>1</td>
                    <td>
                        <input type="text" name="" id="" value="" />
                    </td>
                    <td>
                        <select type='select' name="" class="select" id="select1" multiple="multiple" style="width: 100%;">

                        </select>
                    </td>
                    <td>
                        <input type="text" name="" id="" value="" />
                    </td>                    
                    <td>
                         <input type="text" hidden value="0">
                    </td>   
                    <td>
                         <input type="text" hidden value="0">
                    </td>   
                     <td>
                        <input type="button" class="remove" data-toggle="modal" onclick="delMortgageInfo(this)" title="点击删除" name="" id="" value="删除" />
                        <input type="button" class="add" title="点击添加" name="" id="" value="添加" />
                    </td>
                </tr>
 </table>
            <p class="continueAdd">继续添加抵押本金</p>
            <div class="cancelInConfirm clearfix">
                <input id="ajaxMortgageData" class="cancelInConfirmActive" type="button" value="保存" />
                <input id='cancelMortgageData' type="button" value="取消" />
            </div>
DIYANULL;
            }
            ?>
        </form>
        <!--<div class="topimg top30">
        </div>-->
    </div>
</div>
<script>
    //删除抵押信息
    function delMortgageInfo(obj) {
        var ppes_id = $(obj).parent().prev().find('input').val();
        var mortgage_id = $(obj).parent().prev().prev().find('input').val();
//        thisTd = ;
        var url = '/project/ajax-del-mortgage-project';
        $.post(url,{'project_id':project_id,'ppes_id':ppes_id,'mortgage_id':mortgage_id},function (res) {
            if (res){
                layer.msg('删除成功');
                $(obj).parent().parent().remove()
            }else{
                layer.msg('删除成功');
                $(obj).parent().parent().remove()
            }
        })
    }
    $(function () {
        var mortgage_principal = {};  //抵押本金
        var mortgage_interest = {};   //抵押利息

        var mortgage_owner = {};      //权属人
        var mortgage_type = {};      //抵押物类型
        var mortgage_code = {};      //抵押物编号
        var mortgage_arr = {};
        var xnum = 0;
        var numI = 0;
        var mortgage_arr_c = {} //重组的数据
        var data = [{id: 1, text: '房产'}, {id: 2, text: '土地'}, {id: 3, text: '林矿'}, {id: 4, text: '交通运输'}, {id: 5, text: '机械设备'}, {id: 6, text: '股权'}, {id: 7, text: '无形资产'}, {id: 8, text: '其他资产'}];
        //初始化select2
        var select2JsonNum = 0;
        $("#diYaList").find('.tableEditInfoNum').each(function (i,v) {
            $(".tableEditInfoNum").eq(i).find('tbody tr').each(function (index,val) {
                if (index>0){
                    select2JsonNum++;
                    var selectNum = "mortgageSelect"+select2JsonNum;
                    var selectsId = $("#"+selectNum).data('val');
                    $("#"+selectNum).select2({
                        data:data,
                        width: '320px',
                    }).val(selectsId).trigger('change');
                }
            })
        })
        //点击保存
        $("#ajaxMortgageData").click(function () {
            //获取table抵押本金与利息
            $("#diYaList").find('.tableEditNum').each(function (i,v) {
                mortgage_principal[i] = $(this).find('input:eq(0)').val()==''?0.00:$(this).find('input:eq(0)').val();
                mortgage_interest[i] = $(this).find('input:eq(1)').val()==''?0.00:$(this).find('input:eq(1)').val();
            })
            //获取table抵押信息
            var select2Num = 0;
            var mortgageId = 0;
            $("#diYaList").find('.tableEditInfoNum').each(function (i,v) {
                xnum = i;
                mortgage_arr[xnum] = {};
                $(".tableEditInfoNum").eq(i).find('tbody tr').each(function (index,val) {
                    if (index>0){
                        select2Num++;
                        //如果同一个table，新添加td默认id为0
                        //判断table中有么有数据,有数据则长度大于1，如果有数据默认为有数据的id
                        if (index == 1){
                             mortgageId = $(this).find('input:eq(3)').val();
                        }
                        mortgage_arr[xnum][select2Num] = {};
                        mortgage_arr[xnum][select2Num]['mortgage_owner'] = $(this).find('input:eq(0)').val();
                        mortgage_arr[xnum][select2Num]['mortgage_type'] = $(val).find('td:eq(2)').find('select').val()==[]?'':$(val).find('td:eq(2)').find('select').val().join(',');
                        mortgage_arr[xnum][select2Num]['mortgage_code'] =$(this).find('input:eq(2)').val();
                        mortgage_arr[xnum][select2Num]['mortgage_id'] = mortgageId
                        mortgage_arr[xnum][select2Num]['is_add'] = $(this).find('input:eq(3)').val(); //判断是否为0  非零的更新
                        mortgage_arr[xnum][select2Num]['ppesid'] =$(this).find('input:eq(4)').val();
                        mortgage_arr[xnum][select2Num]['mortgage_order'] =$(this).find('td:eq(0)').text();
                    }
                })
            })
            var data = {
                'mortgage_arr':mortgage_arr,
                'mortgage_principal':mortgage_principal,
                'mortgage_interest':mortgage_interest,
            }
            var url = '/project/ajax-save-mortgage-project';
            $.post(url,{'data':data,'project_id':project_id},function (res) {
                if (res){
                    layer.msg('保存成功');
                    diyawoW();
                }else{
                    layer.msg("更新失败");
                }
            })
        })
        //-----------------渲染无编辑页面--------------------
        function diyawoW() {
            $.get('/project/get-mortgage-view',{'project_id':project_id,'postion':5},function (res) {
                $(".diyawoW").remove();
                $(".diyawuInfoHtml").prepend(res);
                window.location.reload();
            })
        }
        //-----取消保存按钮
        $("#cancelMortgageData").click(function () {
            $.post('/project/ajax-un-lock',{'postion':15,'project_id':project_id},function (res) {
                var JSONData = JSON.parse(res)
                if (JSONData.code == 200){
                    layer.msg(JSONData.message);
                }else{
                    layer.msg(JSONData.message);
                }
            });
            $(".diyawuEdit").hide();
            $(".diyawuW").show();
            $("#getPingGuData").show();
        })


        //前端验证开始
        $.validator.setDefaults({
            debug: true
        })
        $("#diYaList").validate({});
        //前端验证结束
    })

</script>

